<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\member\controller;

/** 控制器类引入区 **/
use app\common\model\ViperUser as ViperUserM;
use app\common\model\Order as OrderM;
use app\common\controller\Wxpay;
/** 模型类引入区 **/

use app\common\model\ProductOrder;
use app\common\model\ViperUser;
use think\Db;

/** 核心或第三方类引入区 **/

/**
 * 支付 控制器
 * @package app\index\controller
 */
class Payment extends Base
{
    public $noNeedLogin = [''];

    /**
     *  项目与商品支付
    */
    public function orderPay()
    {
        $user_id        = $this->user_id;
        $cart_type      = $this->request->post('cart_type');    //类型:  1=门店项目   1=门店商品
        $way            = $this->request->post('way','mp');
        $order_id       = $this->request->post('order_id',0);

        if(empty($order_id))    $this->result('请选择订单','',204);

        switch ($cart_type)
        {
            case 1:
                $orderInfo = OrderM::where(['id'=>$order_id,'uv_id'=>$user_id])->find();
                break;
            case 2:
                $orderInfo = ProductOrder::where(['id'=>$order_id,'uv_id'=>$user_id])->find();
                break;

        }

        if(empty($orderInfo))
        {
            $this->result('订单不存在','',204);
        }

        if($orderInfo['pay_status'] == 1){
            $this->result('订单已支付','',204);
        }


        switch ($cart_type)
        {
            case 1:
                $pay_sn   = p_order_sn(1);
                OrderM::where('id',$orderInfo['id'])->update(['pay_no'=>$pay_sn]);


                $content    = '购买项目';
                $url        = 'member/notify/wx_pj_notify_url';
                break;
            case 2:
                $pay_sn   = p_order_sn(2);
                ProductOrder::where('id',$orderInfo['id'])->update(['pay_no'=>$pay_sn]);

                $content    = '购买商品';
                $url        = 'member/notify/wx_pd_notify_url';
                break;

        }

        $userInfo = ViperUser::where('id',$user_id)->find();
        if(empty($userInfo['openid']))
        {
            $this->result('请登录','',204);
        }
        $Wxpay  =   new Wxpay();
        $res    =   $Wxpay->index($pay_sn,$orderInfo['order_amount'],$content,$userInfo['openid'],$way,$url);
        return $res;


    }


    /**
     *  房间码->  自助结账支付
     */
    public function selfPay()
    {
        $user_id        = $this->user_id;
        $way            = $this->request->post('way','mp');
        $order_id       = $this->request->post('order_id',0);
        if(empty($order_id))    $this->result('请选择订单','',204);

        $orderInfo = Db::name('qt_parent_order')->where(['id'=>$order_id,'uv_id'=>$user_id])->find();

        if(empty($orderInfo))
        {
            $this->result('订单不存在','',204);
        }

        if($orderInfo['pay_status'] == 1)
        {
            $this->result('订单已支付','',204);
        }

        $pay_sn   = p_order_sn(3);
        Db::name('qt_parent_order')->where('id',$orderInfo['id'])->update(['pay_no'=>$pay_sn]);

        $userInfo = ViperUser::where('id',$user_id)->find();
        if(empty($userInfo['openid']))
        {
            $this->result('请登录','',204);
        }

        $Wxpay  =   new Wxpay();
        $res    =   $Wxpay->index($pay_sn,$orderInfo['order_amount'],'自助结账',$userInfo['openid'],$way,'member/notify/self_wx_notify_url');
        return $res;


    }



}
